﻿using DryIoc;
using Microsoft.Win32;
using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLinePacking.Enums;
using ZoneLinePacking.Models.Entity.BaseInfo;
using ZoneLinePacking.Models.Entity.Template;
using ZoneLinePacking.Services.DB;
using ZoneLinePacking.Services.Message;
using ZoneLinePacking.ViewModelBase;

namespace ZoneLinePacking.ViewModels.Dialogs {
    public class EditTemplateDialogViewModel : DialogViewModel {

        public EditTemplateDialogViewModel(
            IRegionManager regionManager, 
            IDialogService dialog, 
            IEventAggregator eventAggregator, 
            IMessageService message, 
            IDbOperate<IFreeSqlMainContext> mainDbOperate) 
            : base(regionManager, dialog, eventAggregator, message, mainDbOperate) {

            TemplateEntity = new TemplateEntity();
        }

        private ObservableCollection<ProductEntity> _products = new ObservableCollection<ProductEntity>();

        public ObservableCollection<ProductEntity> Products {
            get { return _products; }
            set { _products = value; base.RaisePropertyChanged(); }
        }

        private TemplateEntity _templateEntity;
      
        public TemplateEntity TemplateEntity {
            get { return _templateEntity; }
            set { _templateEntity = value; base.RaisePropertyChanged(); }
        }

        private eTemplateType _templateType;

        public eTemplateType TemplateType {
            get { return _templateType; }
            set { _templateType = value; base.RaisePropertyChanged(); }
        }

        public DelegateCommand OpenFileCommand => new DelegateCommand(() => {
            FileDialog dialog = new OpenFileDialog();
            var res = dialog.ShowDialog();
            if (res.HasValue && !string.IsNullOrEmpty(dialog.FileName)) {
                TemplateEntity.TemplatePath = dialog.FileName;
            }
        });

        public DelegateCommand SaveCommand => new DelegateCommand(() => {
            int affected = 0;

            TemplateEntity.TemplateType = TemplateType;

            if (TemplateEntity.ProductID == 0) {
                _message.Warning("请选择产品！");
                return;
            }

            if (string.IsNullOrEmpty(TemplateEntity.TemplateName)) {
                _message.Warning("模板名称必填！");
                return;
            }

            if (string.IsNullOrEmpty(TemplateEntity.TemplatePath)) {
                _message.Warning("模板路径必填！");
                return;
            }
            try {

                affected = _mainDbOperate.GetFreeSql().InsertOrUpdate<TemplateEntity>().SetSource(TemplateEntity).ExecuteAffrows();

                if (affected >= 1) {
                    base.OnDialogClosed(ButtonResult.OK);
                } else {
                    _message.Error("编辑失败！");
                }

            } catch (Exception ex) {

                if (ex.Message.Contains("Duplicate entry")) {
                    _message.GrowError("当前产品重复添加模板！");
                } else {
                    _message.GrowError(ex.Message);
                }
            }
        });

        public DelegateCommand CancelCommand => new DelegateCommand(() => {
            base.OnDialogClosed(ButtonResult.Cancel);
        });

        protected override void OnViewLoaded() {
            var list = _mainDbOperate.GetModelList<ProductEntity>(o => true);
            Products.Clear();
            Products.AddRange(list);

            if (_dialogParameters.ContainsKey("TemplateType")) {
                TemplateType = _dialogParameters.GetValue<eTemplateType>("TemplateType");
            }

            if (_dialogParameters.ContainsKey("TemplateEntity")) {

                TemplateEntity = JsonConvert.DeserializeObject<TemplateEntity>(_dialogParameters.GetValue<string>("TemplateEntity"));

            }
        }


    }
}
